/*
     Copyright (C) 2011 Benny Mackney

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Particle.h"
Particle::Particle()
{
    //ctor
    position = Vec2D(0,0);
    velocity = Vec2D(0,0);
    acceleration = Vec2D(0,0);
    boxRect = {0,0,1,1};//Defines the position/dimensions of the box or position of the sprite
    clipRect = {0,0,1,1};//Define the clipping mask of the sprite
    boxColour = {255,255,255,255};
    sprite = NULL;
    age = 0;
    ageDie = 10;
    set_mode(BSC_BOXMODE_DRAWCENTERED,true);
    set_mode(BSC_BOXMODE_FREESPRITE, false);
    colourmode = false;
}
Particle::Particle(Sint16 X,Sint16 Y, double XVEL, double YVEL, int AGEDIE)
{
    //ctor
    position = Vec2D(X,Y);
    velocity = Vec2D(XVEL,YVEL);
    acceleration = Vec2D(0,0);
    boxRect = {0,0,1,1};//Defines the position/dimensions of the box or position of the sprite
    clipRect = {0,0,1,1};//Define the clipping mask of the sprite
    boxColour = {255,255,255,255};
    sprite = NULL;
    age = 0;
    ageDie = AGEDIE;
    set_mode(BSC_BOXMODE_DRAWCENTERED,true);
    set_mode(BSC_BOXMODE_FREESPRITE, false);
    colourmode = false;
}
Particle::Particle(Sint16 X,Sint16 Y, double XVEL, double YVEL, double XACC, double YACC, int AGEDIE)
{
    //ctor
    position = Vec2D(X,Y);
    velocity = Vec2D(XVEL,YVEL);
    acceleration = Vec2D(XACC,YACC);
    boxRect = {0,0,1,1};//Defines the position/dimensions of the box or position of the sprite
    clipRect = {0,0,1,1};//Define the clipping mask of the sprite
    boxColour = {255,255,255,255};
    sprite = NULL;
    age = 0;
    ageDie = AGEDIE;
    set_mode(BSC_BOXMODE_DRAWCENTERED,true);
    set_mode(BSC_BOXMODE_FREESPRITE, false);
    colourmode = false;
}

Particle::Particle(Vec2D POS, Vec2D VEL, int AGEDIE)
{
    //ctor
    position = POS;
    velocity = VEL;
    boxRect = {0,0,1,1};//Defines the position/dimensions of the box or position of the sprite
    clipRect = {0,0,1,1};//Define the clipping mask of the sprite
    boxColour = {255,255,255,255};
    sprite = NULL;
    age = 0;
    ageDie = AGEDIE;
    set_mode(BSC_BOXMODE_DRAWCENTERED,true);
    set_mode(BSC_BOXMODE_FREESPRITE, false);
    colourmode = false;
}

Particle::Particle(Vec2D POS, Vec2D VEL, Vec2D ACC, int AGEDIE)
{
    //ctor
    position = POS;
    velocity = VEL;
    acceleration = ACC;
    boxRect = {0,0,1,1};//Defines the position/dimensions of the box or position of the sprite
    clipRect = {0,0,1,1};//Define the clipping mask of the sprite
    boxColour = {255,255,255,255};
    sprite = NULL;
    age = 0;
    ageDie = AGEDIE;
    set_mode(BSC_BOXMODE_DRAWCENTERED,true);
    set_mode(BSC_BOXMODE_FREESPRITE, false);
    colourmode = false;
}

Particle::~Particle()
{
    //dtor
    /*if(sprite){
        SDL_FreeSurface(sprite);
    }*/
}

void Particle::tick(){
    velocity+=acceleration;
    position+=velocity;
    boxRect.x = position.get_x();
    boxRect.y = position.get_y();
    age++;
    if(colourmode){
        double progress=(double)age/(double)ageDie;
        int rStart = (*startColour).r;
        int gStart = (*startColour).g;
        int bStart = (*startColour).b;
        int rEnd = (*endColour).r;
        int bEnd = (*endColour).b;
        int gEnd = (*endColour).g;
        int rDiff = (rEnd-rStart)*progress;
        int bDiff = (bEnd-bStart)*progress;
        int gDiff = (gEnd-gStart)*progress;
        boxColour = {rStart+rDiff,gStart+gDiff,bStart+bDiff,0};
    }
    return;
}

void Particle::set_vel(int XVEL,int YVEL){
    velocity.set(XVEL,YVEL);
}

void Particle::set_acceleration(double XACC,double YACC){
    acceleration.set(XACC,YACC);
}

bool Particle::dead(){return (age>ageDie);}

void Particle::set_colourmode(bool mode){
    colourmode = mode;
}
void Particle::set_startcolour(SDL_Colour* colour){
    startColour = colour;
    boxColour=*startColour;
}
void Particle::set_endcolour(SDL_Colour* colour){
    endColour = colour;
}
